// 云函数入口文件
const cloud = require('wx-server-sdk')
const md5 = require('md5-node')

cloud.init()

const db = cloud.database();
const usersTable = db.collection('users');
const _ = db.command;

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()

  if(event.update == true){
    //更新用户
    try{
      return await usersTable.doc(md5(wxContext.OPENID)).update({
        data:{
          userData:_.set(event.userData)
        }
      })
    } catch(err){
      console.error(err)
    }
  }else if(event.getSelf == true){
    //获取当前用户信息
    try{
      return await usersTable.doc(md5(wxContext.OPENID)).field({
        openid:false
      }).get()
    } catch (err) {
      console.error(err)
    }
  } else if (event.setSelf == true) {
    //添加当前用户信息
    try {
      return await usersTable.add({
        data: {
          _id: md5(wxContext.OPENID),
          openid: wxContext.OPENID,
          userData: event.userData,
          boughtList: [],
          messageList: [],
          ontransList: []
        }
      })
    } catch (e) {
      console.error(e)
    }
  }  else if(event.getOthers == true){
    //查看指定用户信息
    try{
      return await usersTable.doc(event.userId).field({
        userData: true
      }).get()
    } catch (err) {
      console.error(err)
    }
  }

 
}